package com.tedu.checkIn.service.impl;

import com.tedu.checkIn.mapper.BedMapper;
import com.tedu.checkIn.mapper.BelongingsMapper;
import com.tedu.checkIn.pojo.dto.BedQuery;
import com.tedu.checkIn.pojo.dto.BelongingsParam;
import com.tedu.checkIn.pojo.dto.BelongingsQuery;
import com.tedu.checkIn.pojo.entity.Belongings;
import com.tedu.checkIn.pojo.vo.BedVO;
import com.tedu.checkIn.pojo.vo.BelongingsVO;
import com.tedu.checkIn.service.BelongingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BelongingsServiceImpl implements BelongingsService {
    @Autowired
    BelongingsMapper belongingsMapper;
    @Autowired
    BedMapper bedMapper;

    @Override
    public void saveBelongings(BelongingsParam belongingsParam) {
        log.debug("保存个人物品业务，参数={}",belongingsParam);
        Belongings belongings = new Belongings();
        BeanUtils.copyProperties(belongingsParam,belongings);
        if(belongings.getId() == null ){
            belongings.setCreateTime(new Date());
            belongingsMapper.insertBelongings(belongings);
        }else {//
            belongings.setUpdateTime(new Date());
            belongingsMapper.updateBelongings(belongings);
        }
    }

    @Override
    public List<BelongingsVO> selectBelongings(BelongingsQuery belongingsQuery) {
        log.debug("查询个人物品业务，参数={}",belongingsQuery);
        List<BelongingsVO> list=belongingsMapper.selectBelongings(belongingsQuery);
        for (BelongingsVO belongingsVO : list) {
            BedQuery bedQuery = new BedQuery();
            bedQuery.setId(belongingsVO.getBedId());
            List<BedVO> bedList = bedMapper.selectBed(bedQuery);
            belongingsVO.setBedName(bedList.get(0).getName());
        }
        log.debug("查询个人物品业务，入数={},出参{}",belongingsQuery,list);
        return list;
    }

    @Override
    public void deleteBed(Integer id) {
        log.debug("删除个人物品业务，参数={}",id);
        belongingsMapper.deleteBelongings(id);
    }
}
